# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>
# Contributor: Mario Finelli <mario at finel dot li>

_gemname='rubocop-rake'
pkgname="ruby-${_gemname}"
pkgver=0.6.0
pkgrel=2
pkgdesc='A RuboCop plugin for Rake'
arch=('any')
url="https://github.com/rubocop/${_gemname}"
license=('MIT')
depends=('ruby' 'rubocop')
makedepends=()
checkdepends=('ruby-parallel' 'ruby-rspec')
options=('!emptydirs')
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('e98233e5e0e8c4a9e03184db6e71c3c470f6fc301b8031d9bae6bb26f140b2169ae78a3ced35c34defb33b63489acb315b45d145f946aefb671b1edcc459b9c8')
b2sums=('902b026a68629df848716a37bf759d30a4d10219955d3bd979413ba9245c8a9af2b0511c9f489f7e5d501df2bf1eb9351868d8955e10cd628fed6405d8088797')

prepare() {
  cd "${_gemname}-${pkgver}"

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec"

  # we don't build from a git checkout
  sed --in-place --regexp-extended 's|git ls-files -z|find . -type f -not -path "*/\.git/*" -print0|' "${_gemname}.gemspec"
}

build() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  gem build "${_gemname}.gemspec"

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install/${_gemdir}" \
    --bindir "tmp_install/usr/bin" \
    "${_gemname}-${pkgver}.gem"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "tmp_install/${_gemdir}/cache/" \
    "tmp_install/${_gemdir}/gems/${_gemname}-${pkgver}/vendor/" \
    "tmp_install/${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"

  find "tmp_install/${_gemdir}/gems/" \
    -type f \
    \( \
      -iname "*.o" -o \
      -iname "*.c" -o \
      -iname "*.so" -o \
      -iname "*.time" -o \
      -iname "gem.build_complete" -o \
      -iname "Makefile" \
    \) \
    -delete

  find "tmp_install/${_gemdir}/extensions/" \
    -type f \
    \( \
      -iname "mkmf.log" -o \
      -iname "gem_make.out" \
    \) \
    -delete
}

check() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  GEM_HOME="tmp_install/${_gemdir}" rake spec
}

package() {
  cd "${_gemname}-${pkgver}"

  cp --archive --verbose tmp_install/* "${pkgdir}"

  install --verbose -D --mode=0644 LICENSE* --target-directory "${pkgdir}/usr/share/licenses/${pkgname}"
  install --verbose -D --mode=0644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
